package com.changgou.web.order.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.WxPayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @Author: HuangFu
 * @Date: 2020/2/19 16:31
 * @Description:
 */
@Controller
@RequestMapping("/wxpay")
public class PayController {

    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private WxPayFeign wxPayFeign;

    @GetMapping
    public String toPayPage(String orderId, Model model){

        //查询订单
        Order order = (Order) orderFeign.findById(orderId).getData();
        if(order == null){
            return "fail";
        }
        //判断当前订单是否支付
        if(!"0".equals(order.getPayStatus())){
            return "fail";
        }

        //默认发起微信支付
        Result<Map> mapResult = wxPayFeign.nativePay(orderId, order.getPayMoney());
        if(mapResult.getData() == null){
            return "fail";
        }

        //设置数据
        Map payMap = mapResult.getData();
        payMap.put("orderId",orderId);
        payMap.put("payMoney",order.getPayMoney());
        model.addAllAttributes(payMap);

        return "wxpay";
    }

    /**
     * 成功页面跳转
     * @param payMoney
     * @param model
     * @return
     */
    @GetMapping("/paysuccess")
    public String topaysuccess(String payMoney,Model model){
        model.addAttribute("payMoney",payMoney);
        return "paysuccess";
    }

}
